package com.ruoyi.tcp.util;

/**
 * @author 蒋翔 SY100242
 * @date 2024/8/3 8:40
 * @email SY100242@hnlens.com
 */
public class CrcUtil {


    public static byte[] getCrc(byte[] data) {
        int crc = 0xffff;
        for (int i = 0; i < data.length; i++) {
            crc ^= data[i] & 0xFF;
            for (int j = 0; j < 8; j++) {
                int tmp = crc & 0x0001;
                crc = crc >> 1;
                if (tmp != 0) {
                    crc = crc ^ 0xA001;
                }
            }
        }
        byte[] res = new byte[2];
        res[0] = (byte) ((crc >> 8) & 0xFF);
        res[1] = (byte) (crc & 0xFF);
        return res;
    }
}
